﻿@model GolfLeague.EditModels.MatchEditModel

<script src="@Url.Content("~/Content/scripts/jquery.timeentry.min.js")" type="text/javascript"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('#dialog').dialog({
            autoOpen: false,
            model: true
        });
        $('input:submit').button();
        $('input:reset').button();
        $.timeEntry.setDefaults({ ampmPrefix: ' ', spinnerImage: '' });
        $('#MatchTime').timeEntry();
        $('div#buttonlist a').hover(
		    function () { $(this).addClass('ui-state-hover'); },
		    function () { $(this).removeClass('ui-state-hover'); }
	    );
    });

    $(function () {
        $(".DeleteButton").click(function (e) {
            e.preventDefault();
            var recordToDelete = $(this).attr("id");
            recordToDelete = recordToDelete.replace("delete-", "");

            $("#dialog").dialog("option", "buttons", {
                "Yes": function () {
                    $(this).dialog("close");
                    if (recordToDelete != '') {
                        $.post("/Match/MatchDelete", { "matchID": recordToDelete },
                        function (data) {
                            $('#row-' + recordToDelete).fadeOut('slow');
                        });
                    }
                },
                "No": function () {
                    $(this).dialog("close");
                    return false;
                }
            });
            $("#dialog").dialog("open");
        });
    });

    $(function () {
        $(".EditButton").click(function (e) {
            e.preventDefault();
            var recordToEdit = $(this).attr("id");
            recordToEdit = recordToEdit.replace("edit-", "");

            if (recordToEdit != '') {
                window.location = '/Match/MatchEdit/' + recordToEdit;
            }
        });
    });

    $(function () {
        $('.CancelEditButton').click(function (e) {
            e.preventDefault();
            var roundID = $(this).attr('id');
            roundID = roundID.replace('cancel-', '');
            window.location = '/Match/Index/' + roundID;
        });
    });

</script>
@{
    ViewBag.Title = "Matches";
}
@Html.ActionLink("Admin Home", "Admin", "Home") > @Html.ActionLink("Schedules", "Index", "Schedule") > @Html.ActionLink("Rounds", "Index", "Round", new { id = Model.Round.ScheduleID }, "") > Matches<br /><br />
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Add Match</legend>

        @Html.HiddenFor(model => model.NewMatch.MatchID)
        @Html.HiddenFor(model => model.NewMatch.RoundID)

        <div class="editor-label">
            @Html.LabelFor(model => model.NewMatch.MatchTime)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.MatchTime)
            @Html.HiddenFor(model => model.NewMatch.MatchTime)
            @Html.ValidationMessageFor(model => model.NewMatch.MatchTime)
        </div>

        <table class="formTable">
            <tr>
                <td class="formTable">
                    <div class="editor-label">
                        Team A
                    </div>
                    <div class="editor-field" style="display:inline">
                        @Html.DropDownListFor(model => model.TeamA, new SelectList(Model.TeamDropdown, "dropdownID", "dropdownText"), new { size = "6" })
                        @Html.ValidationMessageFor(model => model.TeamA)
                    </div>
                </td>
                <td class="formTable">
                    <div class="editor-label">
                        Team B
                    </div>
                    <div class="editor-field">
                        @Html.DropDownListFor(model => model.TeamB, new SelectList(Model.TeamDropdown, "dropdownID", "dropdownText"), new { size = "6" })
                        @Html.ValidationMessageFor(model => model.TeamB)
                    </div>
                </td>
            </tr>
        </table>
        <p>
        @if (Model.NewMatch.MatchID < 1)
        {
            <input type="submit" value="Add" />
        }
        else
        {
            <input type="submit" value="Save" />
            <input type="reset" class="CancelEditButton" value="Cancel" id="cancel-@Model.Round.RoundID" />
        }
        </p>
        <div id="dialog" title="Confirm Delete">Are you sure you want to delete this match?</div>
    </fieldset>
}


<fieldset>
    <legend>Matches for @String.Format("{0:MM/dd/yyyy}", Model.Round.RoundDate)</legend>
    <table>
        <tr>
            <th>
                Match Time
            </th>
            <th>
                Team A
            </th>
            <th>
                Team B
            </th>
            <th></th>
        </tr>

@foreach (var item in Model.MatchesView)
{
        <tr id="row-@item.MatchID">
            <td>
                @if (item.MatchTime.Hour < 12)
                {
                    @String.Format("{0:h':'mm} {1}", item.MatchTime, "AM")
                }
                else
                {
                    @String.Format("{0:h':'mm} {1}", item.MatchTime.Subtract(new TimeSpan(12, 0, 0)), "PM")
                }
            </td>
            <td>
                @item.Team1.TeamName<br />
                (@item.Team1.PlayerTeams.Where(t => t.TeamID == item.Team1.TeamID).First().Player.LastName/@item.Team1.PlayerTeams.Where(t => t.TeamID == item.Team1.TeamID).Last().Player.LastName)
            </td>
            <td>
                @item.Team2.TeamName<br />
                (@item.Team2.PlayerTeams.Where(t => t.TeamID == item.Team2.TeamID).First().Player.LastName/@item.Team2.PlayerTeams.Where(t => t.TeamID == item.Team2.TeamID).Last().Player.LastName)
            </td>
            <td>
                <div id="buttonlist" style="vertical-align:middle">
                    <a href="#" title="Edit Match" class="EditButton ui-state-default ui-corner-all ui-icon ui-icon-pencil" id="edit-@item.MatchID"></a>
                    <a href="#" title="Delete Match" class="DeleteButton ui-state-default ui-corner-all ui-icon ui-icon-trash" id="delete-@item.MatchID"></a>
                </div>
            </td>
        </tr>
}

    </table>
</fieldset>